<html>
<head><meta charset="utf-8"><title>Uninitialized memory gone wrong in C/C++ · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html">Uninitialized memory gone wrong in C/C++</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="170833221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170833221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170833221">(Jul 14 2019 at 12:59)</a>:</h4>
<p>For a WIP blog post, I came up with this example to show that uninitialized memory is weird: <a href="https://godbolt.org/z/JX4B4N" target="_blank" title="https://godbolt.org/z/JX4B4N">https://godbolt.org/z/JX4B4N</a></p>



<a name="170833224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170833224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170833224">(Jul 14 2019 at 12:59)</a>:</h4>
<p><code>make_true</code> returns false, which should be "impossible" no matter which bit pattern <code>x</code> has</p>



<a name="170833227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170833227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170833227">(Jul 14 2019 at 12:59)</a>:</h4>
<p>I'd like to also have a C or C++ version of this, but the "obvious" translation does not get optimized to return 0. Does someone know a way to trigger that?</p>



<a name="170834310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170834310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170834310">(Jul 14 2019 at 13:34)</a>:</h4>
<p>the best I could come up with so far is <a href="https://godbolt.org/z/PvZGQB" target="_blank" title="https://godbolt.org/z/PvZGQB">https://godbolt.org/z/PvZGQB</a></p>



<a name="170834313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170834313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170834313">(Jul 14 2019 at 13:34)</a>:</h4>
<p>but having to work with an unknown parameter is annoying</p>



<a name="170836229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170836229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170836229">(Jul 14 2019 at 14:32)</a>:</h4>
<p><code>new unsigned char</code> will initialize to 0</p>



<a name="170836235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170836235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170836235">(Jul 14 2019 at 14:32)</a>:</h4>
<p><code>new</code> calls the default constructor.</p>



<a name="170836287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170836287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170836287">(Jul 14 2019 at 14:34)</a>:</h4>
<p>Wait, it doesn’t…?</p>



<a name="170836431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170836431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170836431">(Jul 14 2019 at 14:38)</a>:</h4>
<p>In that case </p>
<div class="codehilite"><pre><span></span>static bool always_return_true(unsigned char *x) {
    return (*x &gt; 120) || (*x &lt; 120);
}

bool make_true(bool test) {
    unsigned char x;
    return always_return_true(&amp;x);
}
</pre></div>


<p>works for both C++ and C.</p>



<a name="170836618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170836618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170836618">(Jul 14 2019 at 14:44)</a>:</h4>
<p>My bad, <code>x &gt; 120 || x &lt; 120</code> is not the full range.</p>



<a name="170839976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170839976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170839976">(Jul 14 2019 at 16:36)</a>:</h4>
<p>If you are curious, here's that blog post: <a href="https://www.ralfj.de/blog/2019/07/14/uninit.html" target="_blank" title="https://www.ralfj.de/blog/2019/07/14/uninit.html">https://www.ralfj.de/blog/2019/07/14/uninit.html</a></p>



<a name="170839978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170839978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170839978">(Jul 14 2019 at 16:36)</a>:</h4>
<p><span class="user-mention" data-user-id="137587">@Gankro</span> you dont think you could add a link to that one from your uninit post? :D</p>



<a name="170877463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170877463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170877463">(Jul 15 2019 at 09:36)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> <code>new unsigned char ()</code> or <code>new unsigned char {}</code> should value-initialize the memory, without the <code>()</code> and the <code>{}</code> the memory is default-initialized which for <code>unsigned char</code> is uninitialized IIRC (I often mix <code>value-initialized</code> with <code>default-initialized</code> so maybe the names are the other way around)</p>



<a name="170877581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170877581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170877581">(Jul 15 2019 at 09:37)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> my favourite snippet is:</p>
<div class="codehilite"><pre><span></span><span class="n">unsigned</span><span class="w"> </span><span class="n">char</span><span class="w"> </span><span class="n">uninit</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">char</span><span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>The memory allocated by malloc is uninitialized in the C abstract machine. So any C / C++ compiler worth its salt will avoid allocating any memory, and obviously, trying to read it. Since the result will always be uninitialized, there doesn't need to be any of that.</p>



<a name="170877678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170877678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170877678">(Jul 15 2019 at 09:39)</a>:</h4>
<p>People saying "what the hardware does", or in this case, "what the memory allocator does", are missing the point, because it doesn't matter whether the memory allocator returns zeroed memory, or some junk from a previous allocation, or only allocates when the memory is actually touched, e.g., by a read, since in the C abstract machine, none of that needs to happen here.</p>



<a name="170878388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170878388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170878388">(Jul 15 2019 at 09:51)</a>:</h4>
<p>GCC vs Clang: <a href="https://gcc.godbolt.org/z/cqV8Ve" target="_blank" title="https://gcc.godbolt.org/z/cqV8Ve">https://gcc.godbolt.org/z/cqV8Ve</a></p>



<a name="170881909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170881909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170881909">(Jul 15 2019 at 10:49)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> yeah, I was pretty confident that <code>new T</code> calls the default constructor and you seem to confirm it. I just wasn’t aware that the "default constructor" for "unsigned char" leaves data uninitialized :D</p>



<a name="170881921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170881921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170881921">(Jul 15 2019 at 10:49)</a>:</h4>
<p><code>unsigned char</code> doesn't have a default constructor per se IIRC</p>



<a name="170881928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170881928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170881928">(Jul 15 2019 at 10:49)</a>:</h4>
<p>Hence quoted.</p>



<a name="170881930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170881930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170881930">(Jul 15 2019 at 10:49)</a>:</h4>
<p>there are two things, default initialization, and value initialization</p>



<a name="170881934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170881934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170881934">(Jul 15 2019 at 10:49)</a>:</h4>
<p>I always confuse which one is which</p>



<a name="170881994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170881994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170881994">(Jul 15 2019 at 10:50)</a>:</h4>
<p>but for primitive types one is equivalent to <code>int x;</code> and means that <code>x</code> is indeterminate</p>



<a name="170882013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882013">(Jul 15 2019 at 10:50)</a>:</h4>
<p>and that extends to aggregates, e.g. <code>struct S { int x; int y }; S s;</code> where <code>s</code> is also indeterminate.</p>



<a name="170882016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882016">(Jul 15 2019 at 10:50)</a>:</h4>
<p>Well, <code>T x</code> also default-initlializes in C++</p>



<a name="170882042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882042">(Jul 15 2019 at 10:51)</a>:</h4>
<p>but then some aggregates in some situations are "special" and get a default constructor that might do something else</p>



<a name="170882049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882049">(Jul 15 2019 at 10:51)</a>:</h4>
<p>(e.g. if one of the fields has a constructor)</p>



<a name="170882052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882052">(Jul 15 2019 at 10:51)</a>:</h4>
<p>The catch to be aware of is that "default-initialiser/constructor/whatever-you-call-it" for certain types is undef.</p>



<a name="170882065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882065">(Jul 15 2019 at 10:51)</a>:</h4>
<p>yep, there is a special way to "value-initialize", and that is to use <code>()</code> or <code>{}</code> to call the constructor directly.</p>



<a name="170882119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882119">(Jul 15 2019 at 10:52)</a>:</h4>
<p>so to put it better, the constructor always initializes, but <code>T t;</code> doesn't always call the constructor</p>



<a name="170882122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882122">(Jul 15 2019 at 10:52)</a>:</h4>
<p>Which I’m surprised about. <span class="user-mention" data-user-id="120791">@RalfJ</span> that’s a nice thing to jot down onto your C++ impossible to use right list :D</p>



<a name="170882130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882130">(Jul 15 2019 at 10:52)</a>:</h4>
<p>it gets worse</p>



<a name="170882133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882133">(Jul 15 2019 at 10:52)</a>:</h4>
<p><code>T t();</code> is a function declaration</p>



<a name="170882150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882150">(Jul 15 2019 at 10:53)</a>:</h4>
<p>so you end up needing to write crazy syntax to actually avoid the ambiguity, and properly initialize a variable</p>



<a name="170882162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882162">(Jul 15 2019 at 10:53)</a>:</h4>
<p>or with C++11, you can just use <code>T t{};</code> which also initializes</p>



<a name="170882166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882166">(Jul 15 2019 at 10:53)</a>:</h4>
<p>yes. I remember that one. There’s also <code>banana &amp; peach == 0</code> inheritance from C. I used C++ professionally for a few years. I hate it.</p>



<a name="170882175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882175">(Jul 15 2019 at 10:53)</a>:</h4>
<p>but then..... <code>T t{}</code> doesn't always do the same thing either</p>



<a name="170882219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882219">(Jul 15 2019 at 10:54)</a>:</h4>
<p>e.g. if <code>T</code> is an initializer list</p>



<a name="170882233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882233">(Jul 15 2019 at 10:54)</a>:</h4>
<p>so now you can always use <code>T t{}</code> except... in some corner cases</p>



<a name="170882697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882697">(Jul 15 2019 at 11:03)</a>:</h4>
<p>There's also the reverse, calling the destructor the end the lifetime of an object. <code>~int(ptr)</code>and <code>ptr-&gt;~int()</code> do not work but if a template parameter <code>T</code> works out to incidentally be an <code>int</code> it is allowed as <code>ptr-&gt;~T()</code> which they call the pseduo-destructor. At this point I'm fairly certain the syntax of C++ is a game of trivia against the compiler.</p>



<a name="170882894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882894">(Jul 15 2019 at 11:07)</a>:</h4>
<p>it's probably the hardest game of trivia ever, although some of the Rust games of trivia that are online are quiet tricky as well</p>



<a name="170882973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/170882973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#170882973">(Jul 15 2019 at 11:08)</a>:</h4>
<blockquote>
<p>Which I’m surprised about. <span class="user-mention silent" data-user-id="120791">RalfJ</span> that’s a nice thing to jot down onto your C++ impossible to use right list :D</p>
</blockquote>
<p>that list has long overflown, I only collect blog posts about it now^^</p>



<a name="171287648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171287648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171287648">(Jul 19 2019 at 19:43)</a>:</h4>
<p>Actually I have encountered some "C++ is impossible to use correctly" skeptics that claim otherwise and as a consequence do not see the point of Rust. If such a list was written down it would help in that conversation a great deal.</p>



<a name="171323047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171323047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171323047">(Jul 20 2019 at 11:04)</a>:</h4>
<p><span class="user-mention" data-user-id="127617">@Shnatsel</span> I usually point to <a href="https://robert.ocallahan.org/2017/07/confession-of-cc-programmer.html" target="_blank" title="https://robert.ocallahan.org/2017/07/confession-of-cc-programmer.html">https://robert.ocallahan.org/2017/07/confession-of-cc-programmer.html</a> and <a href="https://www.vice.com/en_us/article/a3mgxb/the-internet-has-a-huge-cc-problem-and-developers-dont-want-to-deal-with-it" target="_blank" title="https://www.vice.com/en_us/article/a3mgxb/the-internet-has-a-huge-cc-problem-and-developers-dont-want-to-deal-with-it">https://www.vice.com/en_us/article/a3mgxb/the-internet-has-a-huge-cc-problem-and-developers-dont-want-to-deal-with-it</a></p>



<a name="171323056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171323056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171323056">(Jul 20 2019 at 11:04)</a>:</h4>
<p><a href="https://www.reddit.com/r/rust/comments/cb49lb/coworker_rust_doesnt_offer_anything_c_doesnt/" target="_blank" title="https://www.reddit.com/r/rust/comments/cb49lb/coworker_rust_doesnt_offer_anything_c_doesnt/">https://www.reddit.com/r/rust/comments/cb49lb/coworker_rust_doesnt_offer_anything_c_doesnt/</a> also has some good arguments</p>



<a name="171323633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171323633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171323633">(Jul 20 2019 at 11:22)</a>:</h4>
<p>These are very basic and broad, and do not really back up their claims, so they're not very useful for a discussion in a technical setting.</p>



<a name="171323808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171323808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171323808">(Jul 20 2019 at 11:28)</a>:</h4>
<p>The Chucklefish whitepaper also falls in the same bucket</p>



<a name="171323821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171323821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171323821">(Jul 20 2019 at 11:29)</a>:</h4>
<p>Reddit discussion is more in-depth and makes some good points, but it's not really structured. I wonder if there is a summary "Rust vs modern C++" article somewhere.</p>



<a name="171325245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171325245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171325245">(Jul 20 2019 at 12:17)</a>:</h4>
<p>I am not aware of one. There is always <a href="https://blog.regehr.org/archives/1520" target="_blank" title="https://blog.regehr.org/archives/1520">https://blog.regehr.org/archives/1520</a> for demonstrating the sheer size of the UB problem.</p>



<a name="171338663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171338663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171338663">(Jul 20 2019 at 19:02)</a>:</h4>
<p><span class="user-mention" data-user-id="127617">@Shnatsel</span> but here's another one for the list: <a href="https://twitter.com/erdgeist/status/1151555830623408131" target="_blank" title="https://twitter.com/erdgeist/status/1151555830623408131">https://twitter.com/erdgeist/status/1151555830623408131</a></p>
<div class="inline-preview-twitter"><div class="twitter-tweet"><a href="https://twitter.com/erdgeist/status/1151555830623408131" target="_blank"><img class="twitter-avatar" src="https://pbs.twimg.com/profile_images/61748149/ta_normal.jpg"></a><p>Sometimes c++ is strange. Just fell for

{
  Pinger ping;
  PingController pc(Pinger);
}

wondering why pc's constructor never was called.

Turns out, compilers aggressively interpret everything smelling like a function definition – in this case pc – as one, without warning.</p><span>- Gerd Eist (@erdgeist)</span></div></div>



<a name="171339236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339236">(Jul 20 2019 at 19:20)</a>:</h4>
<p>The most vexing parse can make the development experience unpleasant sometimes, but it's not in any way related to UB or safety. It just causes very puzzling compiler errors (unless it's an RAII guard that is never referenced again, I guess?).</p>



<a name="171339446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339446">(Jul 20 2019 at 19:27)</a>:</h4>
<p>here someone was thinking they had initialized a variable when they had not</p>



<a name="171339447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339447">(Jul 20 2019 at 19:27)</a>:</h4>
<p>that can cause UB</p>



<a name="171339519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339519">(Jul 20 2019 at 19:30)</a>:</h4>
<p><code>pc</code> is not variable and especially not an uninitialized one, it's a declaration of an external function. Using it will likely complain about type mismatches (or else likely fail during linking because no such function exists), not read uninitialized memory.</p>



<a name="171339579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339579">(Jul 20 2019 at 19:31)</a>:</h4>
<p>The most plausible way this causes UB is if something relies on a side effect of the intended constructor call but I see no indication of that in this case and in fact I've never seen that happen (while I have seen dozens of mysterious compiler errors).</p>



<a name="171339643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339643">(Jul 20 2019 at 19:33)</a>:</h4>
<p>IIRC this can lead to you think you have a guard that does some stuff on "drop" but it won't? but maybe I just interpreted it all wrong.</p>



<a name="171339693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171339693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171339693">(Jul 20 2019 at 19:34)</a>:</h4>
<p>Uh yeah, s/constructor/constructor or destructor/g</p>



<a name="171342521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171342521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171342521">(Jul 20 2019 at 21:03)</a>:</h4>
<p>I hit this once as well. Since then I only ever use <code>{}</code> to initialize. As in <code>PingController pc { ping }</code>.</p>



<a name="171342572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/171342572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#171342572">(Jul 20 2019 at 21:04)</a>:</h4>
<p>The only case where this really does go unnoticed, if you’re using this as a guard, as commented above.</p>



<a name="173491904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/173491904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#173491904">(Aug 18 2019 at 15:53)</a>:</h4>
<blockquote>
<p>Uh yeah, s/constructor/constructor or destructor/g</p>
</blockquote>
<p>for completeness' sake, here's how this going wrong looks like in practice:<br>
<a href="https://www.reddit.com/r/programminghorror/comments/3qm1zp/c_i_spend_much_time_to_find_the_mistake/" target="_blank" title="https://www.reddit.com/r/programminghorror/comments/3qm1zp/c_i_spend_much_time_to_find_the_mistake/">https://www.reddit.com/r/programminghorror/comments/3qm1zp/c_i_spend_much_time_to_find_the_mistake/</a></p>



<a name="173492813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/173492813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#173492813">(Aug 18 2019 at 16:20)</a>:</h4>
<p>Evil. Yet another reason why "the mutex owns the data being protected" is the far superior design :)</p>



<a name="173640438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized%20memory%20gone%20wrong%20in%20C/C%2B%2B/near/173640438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uninitialized.20memory.20gone.20wrong.20in.20C.2FC.2B.2B.html#173640438">(Aug 20 2019 at 09:06)</a>:</h4>
<p>Nice find.</p>
<p>@rkruppe: </p>
<blockquote>
<p>The most vexing parse can make the development experience unpleasant sometimes, but it's not in any way related to UB or safety. It just causes very puzzling compiler errors (unless it's an RAII guard that is never referenced again, I guess?).</p>
</blockquote>
<p>Last famous words  <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span> TBH I thought that too</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>